// executor 执行器的实现
// 如何让执行器决定状态转移的处理
class Promise {
    constructor(exector) {
        this.state = 'pending';
        // 成功的值
        this.value = undefined;
        /// 失败的原因
        this.reason = undefined;
        // 异步任务会把结果交给resolve
        let resolve = (value) =>{
            if(this.state === 'pending') {
                console.log('fulfilled被执行')
                this.value = value
                this.state = 'fulfilled'
            }
        }
        let reject = (reason) => {
            if(this.state === 'pending') {
                console.log('rejected被执行')
                this.reason = reason
                this.state = 'rejected'
            }
        }
        try{
            exector(resolve,reject);
        }catch(err){
            reject(err)
        }
    }
    
    // then() {
        
    // }
}

new Promise((resolve,reject)=>{
    //  封装一个异步任务,实现异步变同步
    setTimeout(()=>{
        // console.log(0)
        // reject('aaa')
        // 语法错误
        // throw new Error('出错了')
        resolve(10);
    },1000)
})